package {
	import com.anjue.bitmap.BmpDrawManager;
	import com.anjue.bitmap.event.BmpEvent;
	import com.anjue.bitmap.event.BmpEventManager;
	import com.anjue.bitmap.display.BmpMovieClip;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;

	[SWF(width=960,height=560)]
	public class TestBitmapMovie extends Sprite
	{
		private var petNum:uint = 0;
		private var numTxt:TextField;
		public function TestBitmapMovie()
		{
			init();
		}
		
		private function init():void
		{
//			stage.displayState = StageDisplayState.FULL_SCREEN;
			
			var txt:TextField = new TextField();
			txt.text = "当前实例个数：";
			txt.x = 200;
			txt.y = 20;
			addChild(txt);
			
			numTxt = new TextField();
			numTxt.x = 210+txt.width;
			numTxt.y = txt.y;
			addChild(numTxt);
			
			addBitmap();
			
//			var i:uint = 0;
//			for(i;i<200;i++)
//			{
//				addVectorgraphPet();
//			}
		}
		
		private function addBitmap():void
		{
			BmpEventManager.addEventListener(BmpEvent.COMPLETE,drawComplete);
			var i:uint = 0;
			for(i;i<800;i++)
			{
				BmpDrawManager.draw("pet",new Hat());
				
//				var loader:Loader = new Loader();
//				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadCom);
//				loader.load(new URLRequest("resource/swf/beautifulPet.swf"));
			}
		}
		
		private function loadCom(e:Event):void
		{
			var l:Loader = e.target.loader as Loader;
			var mc:MovieClip = l.content as MovieClip;
			BmpDrawManager.draw("pet",mc.hat,drawComplete);
			l.contentLoaderInfo.removeEventListener(Event.COMPLETE,loadCom);
			l = null;
		}
		
		private function addVectorgraphPet():void
		{
			var display:MovieClip;
			display = new Hat();
			display.x = Math.random()*500+10;
			display.y = Math.random()*490+10;
			display.gotoAndPlay(uint(display.totalFrames*Math.random())+1);
			addChild(display);
			
			petNum++;
			numTxt.htmlText = "<b>"+petNum+"</b>";
		}
		
		private function drawComplete(e:BmpEvent):void
		{
			if(e.key == "pet" || e.key == "wisdom")
			{
				var display:BmpMovieClip = e.data as BmpMovieClip;
				display.x = Math.random()*800+100;
				display.y = Math.random()*400+100;
				display.gotoAndPlay(uint(Math.random()*display.totFrame)+1)
				addChild(display);
			}
			
			petNum++;
			numTxt.htmlText = "<b>"+petNum+"</b>";
		}
	}
}
